home *** CD-ROM | disk | FTP | other *** search
-
- #include "SampleHeader.h"
-
- /* In the 68K world, this is provided for you as part of your A5 world, but */
- /* on the PowerPC, you have to explicitly include it. */
- #ifdef __powerc
- QDGlobals qd;
- #endif
-
- /* Two globals. One always tells me whether I'm in the background or not */
- /* (maintained in the Suspend/Resume event handler in MainEventLoop()), and */
- /* another that allows me to cleanly drop out of my main event loop from */
- /* anywhere. Sure ExitToShell() works, too, but I like to exit in only one */
- /* way, and this global allows me to do that. */
- Boolean gDone;
- Boolean gInBackground;
-
- /*-------------------------------------------------------------------------------------*/
-
- void main()
- {
-
- InitApplication();
- PreEventLoop();
- MainEventLoop();
- }
-
-
- /*-------------------------------------------------------------------------------------*/
- /*
- InitApplication - initializes the toolbox, my globals, and my menu bar.
- */
- void InitApplication()
- {
- Handle theMenu;
-
- // Toolbox initialization
- MaxApplZone();
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
- FlushEvents(0,everyEvent);
-
- // Application initialization
- gDone = false;
- gInBackground = false;
-
- theMenu = GetNewMBar(kMenuBarResID);
- if ( theMenu != nil )
- {
- SetMenuBar(theMenu);
- AddResMenu(GetMHandle(kAppleMenu), 'DRVR');
- DrawMenuBar();
- }
- else
- /* If the menu stuff failed, something just ain't */
- /* right (most likely some resources are missing). */
- gDone = true;
-
- }
-
-
- /*-------------------------------------------------------------------------------------*/
- /*
- DoAboutBox - I just have an Alert dialog box set up in my resources for this.
- */
- void DoAboutBox()
- {
- (void) Alert(kAboutBoxDITLID, nil);
- }
-
-
- /*-------------------------------------------------------------------------------------*/
- /*
- MainEventLoop - handles all events for this application. Note that the first
- thing done with an event is to check if it belongs to a dialog, and if so the
- event is passed to DoDialogEvent() which handles all dialog events except
- null events. To check if it's a dialog event, the IsDialogEvent routine is
- called which checks to see if the frontmost window is a dialog and if the
- event is not an update, a mousedown or an activate for another window. Note
- that null events are all passed to DoDialogNullEvent, which is needed to keep
- the editText cursor blinking..
-
- */
- void MainEventLoop()
- {
- EventRecord theEvent;
- WindowPtr thisWindow;
- short clickArea;
- Rect screenRect;
- long menuResult;
- char charCode;
-
- while ( !gDone )
- {
- if ( WaitNextEvent(everyEvent, &theEvent, 0, nil) )
- {
- if ( IsDialogEvent(&theEvent) == true )
- {
- DoDialogEvent(&theEvent);
- continue;
- }
-
- switch (theEvent.what)
- {
- case mouseDown:
- clickArea = FindWindow(theEvent.where, &thisWindow);
-
- switch ( clickArea )
- {
- case inDrag:
- screenRect = (**GetGrayRgn()).rgnBBox;
- DragWindow(thisWindow, theEvent.where, &screenRect);
- break;
- case inContent:
- if ( thisWindow != FrontWindow() )
- SelectWindow(thisWindow);
- break;
- case inGoAway:
- if ( TrackGoAway(thisWindow, theEvent.where) == true )
- {
- if ( ((WindowRecord *)thisWindow)->windowKind == dialogKind )
- DisposeDialog(thisWindow);
- else
- gDone = true;
- }
- break;
- case inMenuBar:
- AdjustMenus();
- menuResult = MenuSelect(theEvent.where);
- if ( (menuResult >> 16) != 0 )
- (void) MenuCommand(menuResult);
- break;
- }
-
- break;
- case keyDown:
- charCode = theEvent.message & charCodeMask;
-
- if ( (theEvent.modifiers & cmdKey) != 0 )
- {
- AdjustMenus();
- menuResult = MenuKey(charCode);
-
- if ( (menuResult >> 16) != 0 )
- (void) MenuCommand(menuResult);
-
- }
- break;
- case updateEvt:
- thisWindow = (WindowPtr)theEvent.message;
- DoUpdate(thisWindow);
-
- break;
- case app4Evt:
- /* Make sure the SIZE resource reflects that suspend evts are accepted.*/
- if ( theEvent.message & 0x01000000 ) /* Suspend or resume? */
- {
- if ( theEvent.message & 0x00000001 )/* Is it a resume event? */
- {
- gInBackground = false;
- SetCursor(&qd.arrow);
- }
- else /* It's a suspend event */
- gInBackground = true;
-
- }
- break;
- }
- }
- else
- DoDialogNullEvent(&theEvent);
- }
- }
-
-
-
- /*-------------------------------------------------------------------------------------*/
- /*
- MenuCommand - called in response to keydowns in both my dialog event handler and
- the main event loop. Note that because of AdjustMenus(), the edit keys will only
- be hit if the dialog is the frontmost window, which is what we want.
-
- */
- Boolean MenuCommand(long whaHappened)
- {
- short menuID, menuItem;
- Boolean performedEdit = false;
- DialogPtr theDialog;
-
- menuID = (whaHappened >> 16);
- menuItem = (whaHappened & 0xFFFF);
-
- switch ( menuID )
- {
- case kAppleMenu:
- HiliteMenu(kAppleMenu);
- if ( menuItem == 1)
- DoAboutBox();
- break;
-
- case kFileMenu:
- HiliteMenu(kFileMenu);
- switch ( menuItem )
- {
- case kNewItem:
- CreateModelessDialog();
- break;
- case kQuitItem:
- gDone = true;
- break;
- }
- break;
- case kEditMenu:
- // This thing should be a dialog, because the menu items are
- // disabled unless a modeless dialog is frontmost.
- theDialog = (DialogPtr)FrontWindow();
-
- if ( theDialog != nil )
- {
- HiliteMenu(kEditMenu);
- switch ( menuItem )
- {
- case kCutItem:
- DoCut(theDialog);
- performedEdit = true;
- break;
- case kCopyItem:
- DoCopy(theDialog);
- performedEdit = true;
- break;
- case kPasteItem:
- DoPaste(theDialog);
- performedEdit = true;
- break;
- case kClearItem:
- DoClear(theDialog);
- performedEdit = true;
- break;
- }
- }
- break;
- }
- HiliteMenu(0);
- return performedEdit;
- }
-
-
-
-
- /*-------------------------------------------------------------------------------------*/
- /*
- DrawIt - I've included a normal window in this application to show the difference
- between handling events for a dialog and a normal window. This just draws some
- text into the normal window.
-
- */
- void DrawIt(WindowPtr win)
- {
- short origFont, origSize;
-
- origFont = win->txFont;
- origSize = win->txSize;
- TextFont(geneva);
- TextSize(48);
-
- ForeColor(redColor);
- PaintRect(&(*win).portRect);
- ForeColor(blackColor);
-
- MoveTo(10, win->portRect.bottom - 10);
- DrawString((StringPtr)"\pModeless Sample");
-
- TextFont(origFont);
- TextSize(origSize);
- }
-
-
- /*-------------------------------------------------------------------------------------*/
- /*
- DrawWindowContent - called by DeviceLoop in the main event loop. In response to
- update events.
-
- Since I don't do anything different for different depths, grayscale, etc, I just
- ignore those parameters, but you don't have to.
-
- */
- pascal void DrawWindowContent(short pixelDepth, short dFlags, GDHandle theDevice, long theWin)
- {
- #pragma unused (pixelDepth, dFlags, theDevice)
- GrafPtr savePort;
-
- GetPort(&savePort);
- SetPort((GrafPtr)theWin);
-
- DrawIt((WindowPtr)theWin);
-
- SetPort(savePort);
- }
-
-
- /*-------------------------------------------------------------------------------------*/
- /*
- CreateNewWindow - called in PreEventLoop to create the normal non-dialog window.
-
- */
- void CreateNewWindow(void)
- {
- Rect winDimension;
-
- SetRect(&winDimension, 60, 60, 475, 120);
- (void)NewCWindow(0L, &winDimension, (StringPtr)"\pSample", true, noGrowDocProc,
- (WindowPtr)-1L, true, 0L);
- }
-
-
-
- /*-------------------------------------------------------------------------------------*/
- /*
- DoUpdate - called in the main event loop.
-
- */
- void DoUpdate(WindowPtr thisWindow)
- {
- static DeviceLoopDrawingUPP procForDeviceLoop = nil;
-
- SetPort(thisWindow);
-
- if ( procForDeviceLoop == nil )
- procForDeviceLoop = NewDeviceLoopDrawingProc(DrawWindowContent);
-
- BeginUpdate(thisWindow);
- DeviceLoop(thisWindow->visRgn, procForDeviceLoop, (long)thisWindow, singleDevices);
- EndUpdate(thisWindow);
- }
-
-
- /*-------------------------------------------------------------------------------------*/
-
- void PreEventLoop(void)
- {
- CreateNewWindow();
- CreateModelessDialog();
- }
-
-
- /*-------------------------------------------------------------------------------------*/
-
- void PostEventLoop(void)
- {
- }
-
-
- /*-------------------------------------------------------------------------------------*/
- /*
- AdjustMenus - called in whenever command-keys are pressed, or if the menu bar is
- hit with the mouse, so the menus are adjusted only right before they're accessed.
- */
- void AdjustMenus(void)
- {
- MenuHandle theFuncsMenu = GetMenu(kEditMenu);
- WindowPtr theWindow = FrontWindow();
-
- if ( ((WindowRecord *)theWindow)->windowKind != dialogKind )
- {
- DisableItem(theFuncsMenu, kCutItem);
- DisableItem(theFuncsMenu, kCopyItem);
- DisableItem(theFuncsMenu, kPasteItem);
- DisableItem(theFuncsMenu, kClearItem);
- }
- else
- {
- EnableItem(theFuncsMenu, kCutItem);
- EnableItem(theFuncsMenu, kCopyItem);
- EnableItem(theFuncsMenu, kPasteItem);
- EnableItem(theFuncsMenu, kClearItem);
- }
- }
-